<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
    <title>地图显示</title>
    <style>
        html,
        body,
        #container {
            width: 100%;
            height: 100%;
        }

        #hotelBody {
            display: none;
            flex-direction: column;
            position: absolute;
            background-color: white;
            max-height: 90%;
            top: 50px;
            left: 10px;
            width: 280px;
            padding: 10px;
            overflow-y:auto;
            border-radius: 5px;
        }

        .amap_lib_placeSearch {
            min-width: 0px !important;
        }

        .input-card {
            bottom: 1rem;
            left: 40%;
        }

        #chooseButton {
            height: 2rem;
            width: 7rem;
            border: hidden;
            border-radius: 0.25rem;
            color: white;
            background-color: #96bfee;
            cursor: pointer;
            align-self: center;
        }
    </style>
</head>
<body>
    <div id="container"></div>
    <div class="input-card">
        <h4>选择城市</h4>
        <div class="input-item">
            <div class="input-item-prepend"><span class="input-item-text">省市区</span></div>
            <select id='province' style="width:100px" onchange='search(this)'></select>
        </div>
        <div class="input-item">
            <div class="input-item-prepend"><span class="input-item-text">地级市</span></div>
            <select id='city' style="width:100px" onchange='search(this)'></select>
        </div>
        <button id="chooseButton" onclick="chooseCity()">选择城市</button>
    </div>

    <div id="hotelBody">
        <div style="display: flex; align-items: center; margin-bottom: 5%">
            <label for="hotelSearch" style="font-weight: bold; margin: 0">搜索酒店：</label>
            <input id="hotelSearch" type="text" style="width: 70%" onkeydown="searchHotelInList(event);" />
        </div>
        <div id="panel" />
    </div>


    <script type="text/javascript">
        // 使用JSAPI必须搭配密钥
        window._AMapSecurityConfig = {
            // securityJsCode: '0300bab869a7f4717dc44773bbb0f29e',
            securityJsCode: 'de3dabd1ba7ffd802aac72c468609ef5',
        }
    </script>
    <!-- 加载地图JSAPI脚本 -->
    <!--<script src="https://webapi.amap.com/maps?v=2.0&key=5934931986b461f961460bb4b1cb5e32&plugin=AMap.PlaceSearch,AMap.InfoWindow,AMap.MouseTool,AMap.Geocoder,AMap.DistrictSearch"></script>-->
    <script src="https://webapi.amap.com/maps?v=2.0&key=5e8e4187dd44bdb2f345d8f34d76bf0f&plugin=AMap.PlaceSearch,AMap.InfoWindow,AMap.MouseTool,AMap.Geocoder,AMap.DistrictSearch"></script>
    <!--<script src="https://webapi.amap.com/maps?v=2.0&key=ca31a62dc5963d690a81a5eaec1aa9ca&plugin=AMap.PlaceSearch,AMap.InfoWindow,AMap.MouseTool,AMap.Geocoder"></script>-->
    <script src="JsAPI.js"></script>
    <script>
        var map = new AMap.Map('container', {
            viewMode: '2D',
            zoom: 5, // 初始化地图层级
            center: [108.947404, 34.341036], // 初始化地图中心点
            isHotspot: true
        });

        // 添加比例尺
        map.plugin(["AMap.Scale"], function () {
            map.addControl(new AMap.Scale());
        });

        // 缩放按钮
        map.plugin(["AMap.ToolBar"], function () {
            map.addControl(new AMap.ToolBar());
        });

        var placeSearch = new AMap.PlaceSearch();  //构造地点查询类
        var hotelSearch = null; // 酒店搜索
        var infoWindow = new AMap.InfoWindow({});   // 信息窗体
        var mapContextMenu = new AMap.ContextMenu();    // 地图右键菜单
        var geocoder = new AMap.Geocoder({ extensions: 'all' });     // 逆地理解码
        var rightClickPosition; // 鼠标右键点击位置
        var districtSearch = new AMap.DistrictSearch({ subdistrict: 1 }); // 行政区查询
        polylines = [] // 用于标识行政区的边界

        var citySelect = document.getElementById('city');

        districtSearch.search('中国', function (status, result) {
            if (status == 'complete') {
                getData(result.districtList[0]);
            }
        });


        mapContextMenu.addItem("缩放至全国范围", function (e) {
            map.setZoomAndCenter(4, [108.946609, 34.262324]);
        }, 0);

        mapContextMenu.addItem("设为想去的城市并搜索酒店", function (e) {
            geocoder.getAddress(rightClickPosition, function (status, result) {
                if (status === 'complete' && result.info === 'OK') {
                    var cityName = result.regeocode.addressComponent.city;
                    if (cityName.length == 0) {
                        // 直辖市
                        cityName = result.regeocode.addressComponent.province;
                    }
                    var cityCode = result.regeocode.addressComponent.citycode;
                    window.ViewModel.setWish(cityName);
                    searchPicture(cityName);
                    searchHotels(cityCode, map);
                }
            });
        }, 1);

        // 右键呼出菜单
        map.on("rightclick", function (e) {
            mapContextMenu.open(map, e.lnglat);
            rightClickPosition = e.lnglat;
        });

        // 热点信息展示
        map.on('hotspotclick', function (result) {
            placeSearch.getDetails(result.id, function (status, result) {
                if (status === 'complete' && result.info === 'OK') {
                    placeSearch_CallBack(result, map, infoWindow);
                }
            });
        });

        // 搜索并将结果标记在地图上
        function searchKeyWord(keyword) {
            placeSearch.search(keyword, function (status, result) {
                var pois = result.poiList.pois;
                for (var i = 0; i < pois.length; i++) {
                    var poi = pois[i];
                    var marker = makeMarker(map, poi.location);
                    marker.on("rightclick", markerRightClickCallBack);
                    map.add(marker);
                }
                map.setFitView();
            });
        }

        // 搜索城市图片
        function searchPicture(cityName) {
            var pictureSearch = new AMap.PlaceSearch({ extensions: 'all' });
            pictureSearch.search(cityName, function (status, result) {
                var imgUrl = result.poiList.pois[0]['photos'][0]['url'];
                window.ViewModel.setImgURL(imgUrl);
            });
        }

        // 搜索酒店
        function searchHotels(cityCode) {
            hotelSearch = new AMap.PlaceSearch({
                pageSize: 5,
                pageIndex: 1,
                city: cityCode,
                citylimit: true,
                map: map,
                panel: "panel",
                autoFitView: true
            });
            hotelSearch.search('酒店');
            document.getElementById("hotelBody").style.display = "flex";
            hotelSearch.on('markerClick', function (event) {
                window.ViewModel.setHotel(event.data.name, event.data.location.toArray());
            });
            hotelSearch.on('listElementClick', function (event) {
                window.ViewModel.setHotel(event.data.name, event.data.location.toArray());
            });
        }

        // 在列表中搜索酒店
        function searchHotelInList(e) {
            // 回车事件
            if (e.keyCode == 13) {
                var keyword = document.getElementById("hotelSearch").value;
                hotelSearch.search(keyword);
            }
        }

        function getData(data, level) {
            var bounds = data.boundaries;
            if (bounds) {
                for (var i = 0, l = bounds.length; i < l; i++) {
                    var polyline = new AMap.Polyline({
                        map: map,
                        strokeWeight: 4,
                        strokeOpacity: 1,
                        strokeColor: '#7C83FD',
                        path: bounds[i]
                    });
                    polylines.push(polyline);
                }
                map.setFitView();//地图自适应
            }

            //清空下一级别的下拉列表
            if (level === 'province') {
                citySelect.innerHTML = '';
            }

            var subList = data.districtList;
            if (subList) {
                var contentSub = new Option('--请选择--');
                var curlevel = subList[0].level;
                var curList = document.querySelector('#' + curlevel);
                curList.add(contentSub);
                // 添加选项
                for (var i = 0, l = subList.length; i < l; i++) {
                    var name = subList[i].name;
                    var levelSub = subList[i].level;
                    var cityCode = subList[i].citycode;
                    contentSub = new Option(name);
                    contentSub.setAttribute("value", levelSub);
                    contentSub.center = subList[i].center;
                    contentSub.adcode = subList[i].adcode;  // 后续用于搜索
                    contentSub.citycode = cityCode;
                    curList.add(contentSub);
                }
            }
        }

        function search(obj) {
            //清除地图上所有覆盖物
            for (var i = 0, l = polylines.length; i < l; i++) {
                polylines[i].setMap(null);
            }
            var option = obj[obj.options.selectedIndex];
            var keyword = option.text; //关键字
            var adcode = option.adcode;
            districtSearch.setLevel(option.value); //行政区级别
            districtSearch.setExtensions('all');
            //行政区查询
            //按照adcode进行查询可以保证数据返回的唯一性
            districtSearch.search(adcode, function (status, result) {
                if (status === 'complete') {
                    getData(result.districtList[0], obj.id);
                }
            });
        }

        function chooseCity() {
            var option = citySelect[citySelect.options.selectedIndex];
            if (option.innerHTML == "--请选择--") {
                return;
            }
            var cityName = option.innerHTML;
            var cityCode = option.citycode;
            window.ViewModel.setWish(cityName);
            searchPicture(cityName);
            searchHotels(cityCode);
        }
    </script>
</body>
</html>